"""
kg表数据处理器
1.原始数据入库tb_fenbi
2.原始数据去重 入库tb_fenbi_norepeat
3.根据kg表对tb_fenbi_norepeat去重
4.去重后tb_fenbi_norepeat插入kg
"""
from django.db.models import Q
import json
import datetime
from django.db import transaction
import logging

from project.models import Kg, TbFenbiNorepeat, WordKnowledgepoints
from question.models import TbFenbiV2, TbFenbiV2Norepeat

errlog = logging.getLogger("err")


class KgDataProcessor:
    """
    根据kg表对 tb_fenbi_norepeat去重
    """

    def fenbinorepeat_de_weight_by_kg(self):
        print("1.在tb_fenbi_v2_norepeat中去掉kg表中已有的题目")
        before_num = TbFenbiV2Norepeat.objects.all().count()
        kg_query = Kg.objects.all().values()
        kg_query_count = kg_query.count()
        repeat_num = 0
        for i in range(kg_query_count):
            kg_question = kg_query[i]
            fenbi_query = TbFenbiV2Norepeat.objects.filter(stem=kg_question["stem"])
            fenbi_query_count = fenbi_query.count()
            print('\r进度：{}/{} 重复:{}'.format(i + 1, kg_query_count, repeat_num), end="")
            if (fenbi_query_count > 0):
                repeat_num += fenbi_query.count()
                fenbi_query.delete()
        after_num = TbFenbiV2Norepeat.objects.all().count()
        print("\n去重前:{} 去重后：{}".format(before_num, after_num))
        print("==========")

    """
    tb_fenbi_v2_norepeat匹配知识点
    """

    def fenbi_norepeat_chapter_amend(self):
        print("2.tb_fenbi_norepeat重新标注知识点")
        questions = TbFenbiV2Norepeat.objects.all()
        questions_count = questions.count()
        amend_num = 0
        for i in range(questions_count):
            question = questions.values()[i]
            points = WordKnowledgepoints.objects.filter(point_name=question["chapter_name"])
            if (points.count() > 0):
                amend_num += 1
                info = {
                    "chapter_name": points.first().unit
                }
                questions.filter(uid=question["uid"]).update(**info)
            print("\r进度：{}/{} 已匹配知识点：{}".format(i + 1, questions_count, amend_num), end="")
        print("\n==========")

    """
    去重后数据插入kg表
    """

    def insert_into_kg(self):
        print("3.去重后数据插入kg表")
        questions = TbFenbiV2Norepeat.objects.all()
        questions_count = questions.count()
        print("有" + str(questions_count) + "条未插入")
        insert_num = 0
        for i in range(questions_count):
            print("\r进度:{}/{}".format(i + 1, questions_count), end="")
            try:
                with transaction.atomic():
                    question = questions[i]
                    chapter_name = question.chapter_name
                    if chapter_name is None:
                        chapter_name = ""
                    info = {
                        "stem": question.stem,
                        "choices": {
                            "a": question.choice_a,
                            "b": question.choice_b,
                            "c": question.choice_c,
                            "d": question.choice_d,
                        },
                        "choice_a": question.choice_a,
                        "choice_b": question.choice_b,
                        "choice_c": question.choice_c,
                        "choice_d": question.choice_d,
                        "answer": str([question.answer]),
                        "explanation": question.analyze,
                        "source": question.source,
                        "subject": "公务员行测",
                        "chapter": chapter_name,
                        "question_type": question.type,
                        "exam_type": "行测",
                        "question_source": "wf",
                        "is_delete": 0,
                        "is_marked": 0
                    }
                    info = self.add_datetime(info)
                    Kg.objects.create(**info)
                    insert_num += 1
            except Exception as e:
                errlog.error(e)
        print("\n共插入:", insert_num)

    def amend_kg_choice(self):
        kg_questions = Kg.objects.filter(create_time__gte="2023-07-07")
        kg_questions_count = kg_questions.count()
        amend_count = 0
        for i in range(kg_questions_count - 1):
            amend_count += 1
            question = kg_questions.values()[i]
            print('\r进度：{}/{} 已修正：{}'.format(i, kg_questions_count, amend_count), end="")
            info = {
                "choice_a": eval(question["choices"]).get("a"),
                "choice_b": eval(question["choices"]).get("b"),
                "choice_c": eval(question["choices"]).get("c"),
                "choice_d": eval(question["choices"]).get("d"),
            }
            # print(question)
            kg_questions.filter(uid=question["uid"]).update(**info)

    """
    添加日期
    """

    def add_datetime(self, info):
        now = str(datetime.datetime.now())
        info["create_time"] = now
        info["update_time"] = now
        return info

    def run(self):
        self.fenbinorepeat_de_weight_by_kg()
        self.fenbi_norepeat_chapter_amend()
        self.insert_into_kg()


def test():
    KgDataProcessor().run()
